package cn.geminis.workflow.service.service.engine.expression;

import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.common.engine.api.variable.VariableContainer;
import org.flowable.engine.delegate.DelegateExecution;

/**
 * @author puddi
 */
@RequiredArgsConstructor
public class GeminiExpression implements Expression {

    private final Expression expression;
    private final ExpressionValueContext valueContext;

    @Override
    public Object getValue(VariableContainer variableContainer) {
        if (variableContainer instanceof DelegateExecution) {
            var newProcessInstanceId = ((DelegateExecution) variableContainer).getProcessInstanceId();
            this.valueContext.withProcess(newProcessInstanceId);
            this.valueContext.withTask(variableContainer);
        }

        variableContainer = new JsonVariableContainer(this.valueContext.getVariables());
        return expression.getValue(variableContainer);
    }

    @Override
    public void setValue(Object object, VariableContainer variableContainer) {
        this.expression.setValue(object, variableContainer);
    }

    @Override
    public String getExpressionText() {
        return this.expression.getExpressionText();
    }
}
